package com.ctgu.rabbitmq.rabbitmq.exchange.directExchange;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ ClassName DirectEchangeConfig
 * @ Author Alex2
 * @ Date 2025/8/14 14:59
 **/
@Configuration
public class DirectExchangeConfig {
    public static final String DIRECT_EXCHANGE = "direct_exchange";
    public static final String QUEUE_INFO = "queue_info";
    public static final String QUEUE_ERROR = "queue_error";

    // 路由键
    public static final String ROUTING_KEY_INFO = "info";
    public static final String ROUTING_KEY_ERROR = "error";

    // 创建直连交换机
    @Bean(name = DIRECT_EXCHANGE)
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    // 创建队列
    @Bean(name = QUEUE_INFO)
    public Queue queueInfo() {
        return QueueBuilder.durable(QUEUE_INFO).ttl(5000).build();
    }

    @Bean(name = QUEUE_ERROR)
    public Queue queueError() {
        return new Queue(QUEUE_ERROR, true);
    }

    // 绑定队列和交换机（带路由键）
    @Bean
    public Binding bindingQueueInfo(
            @Qualifier(QUEUE_INFO) Queue queue,
            @Qualifier(DIRECT_EXCHANGE) DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(ROUTING_KEY_INFO);
    }

    @Bean
    public Binding bindingQueueError(
            @Qualifier(QUEUE_ERROR) Queue queue,
            @Qualifier(DIRECT_EXCHANGE) DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(ROUTING_KEY_ERROR);
    }
}